package com.jeeplus.test.website.websiteContent.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jeeplus.sys.service.UserService;
import com.jeeplus.test.tools.resp.Convert2Bean;
import com.jeeplus.test.website.websiteContent.domain.entity.WebsiteContent;
import com.jeeplus.test.website.websiteContent.domain.vo.WebsiteContentReqVo;
import com.jeeplus.test.website.websiteContent.domain.vo.WebsiteContentRespVo;
import com.jeeplus.test.website.websiteContent.mapper.WebsiteContentMapper;
import com.jeeplus.test.website.websiteContent.service.WebsiteContentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class WebsiteContentServiceImpl extends ServiceImpl<WebsiteContentMapper, WebsiteContent> implements WebsiteContentService {
    @Autowired
    private WebsiteContentMapper websiteContentMapper;
    @Autowired
    private UserService userService;

    @Override
    public Page<WebsiteContentRespVo> getContentPage(WebsiteContentReqVo websiteContentReqVo, long current, long size) {
        WebsiteContent websiteContent = Convert2Bean.convertBean(websiteContentReqVo, WebsiteContent.class);
        LambdaQueryWrapper<WebsiteContent> queryWrapper = new LambdaQueryWrapper<>();
        if (ObjectUtil.isNotEmpty(websiteContent.getContentName())) {
            queryWrapper.eq(WebsiteContent::getContentName, websiteContent.getContentName());
        }
        if (ObjectUtil.isNotEmpty(websiteContent.getContentSort())) {
            queryWrapper.eq(WebsiteContent::getContentSort, websiteContent.getContentSort());
        }
        if (ObjectUtil.isNotEmpty(websiteContent.getContentStatus())) {
            queryWrapper.eq(WebsiteContent::getContentStatus, websiteContent.getContentStatus());
        }
        queryWrapper.orderByDesc(WebsiteContent::getCreateTime);
        IPage<WebsiteContent> selectResult = websiteContentMapper.selectPage(new Page<>(current, size, true), queryWrapper);
        List<WebsiteContentRespVo> resultList = selectResult.getRecords().stream()
                .map(bean -> {
                    WebsiteContentRespVo websiteContentRespVo = Convert2Bean.convertBean(bean, WebsiteContentRespVo.class);
//                    websiteContentRespVo.setCreateUser(userService.getById(websiteContentRespVo.getCreateUser()).getName());
                    return websiteContentRespVo;
                })
                .collect(Collectors.toList());
        Page<WebsiteContentRespVo> page = new Page<>(current, size, true);
        page.setCurrent(selectResult.getCurrent());
        page.setTotal(selectResult.getTotal());
        page.setRecords(resultList);
        return page;
    }

    @Override
    public WebsiteContentRespVo queryById(String id) {
        WebsiteContent websiteContent = baseMapper.selectById(id);
        return Convert2Bean.convertBean(websiteContent, WebsiteContentRespVo.class);
    }

    @Override
    public Boolean addContent(WebsiteContentReqVo websiteContentReqVo) {
        websiteContentReqVo.setId("WB" + IdUtil.simpleUUID());
        WebsiteContent websiteContent = Convert2Bean.convertBean(websiteContentReqVo, WebsiteContent.class);
        return baseMapper.insert(websiteContent) > 0;
    }

    @Override
    public Boolean updateContent(WebsiteContentReqVo websiteContentReqVo) {
        WebsiteContent websiteContent = Convert2Bean.convertBean(websiteContentReqVo, WebsiteContent.class);
        LambdaUpdateChainWrapper<WebsiteContent> wrapper = new LambdaUpdateChainWrapper<>(websiteContentMapper);
        if (ObjectUtil.isNotEmpty(websiteContent.getContentStatus())) {
            wrapper.set(WebsiteContent::getContentStatus, websiteContent.getContentStatus());
        }
        if (ObjectUtil.isNotEmpty(websiteContent.getContentSort())) {
            wrapper.set(WebsiteContent::getContentSort, websiteContent.getContentSort());
        }
        if (ObjectUtil.isNotEmpty(websiteContent.getContentName())) {
            wrapper.set(WebsiteContent::getContentName, websiteContent.getContentName());
        }
        wrapper.eq(WebsiteContent::getId, websiteContent.getId());
        return wrapper.update();
    }

    @Override
    public Boolean deleteContent(String id) {
        return websiteContentMapper.deleteById(id) > 0;
    }
}
